$(function(){


  function layerCss(){
    $(".layui-layer").css("border-radius","10px")
    $(".layui-layer-setwin .layui-layer-close2").css({"right":"-40px","top":"0px"});
    $(".layui-layer-ico").css("background","url("+jumpUrl+"/img/question2/iconlayer.png)");
  }

	var len = $("#bannerbox li").length; // 你有几div
	var n = 0;
	$("#bannerbox li").eq(0).show(); //默认第一个为显示
	setInterval(function(){
		if(n==0){     // 到了最后一个则又重复执行
			n=len
		}else{
			n--;
			$("#bannerbox li").eq(n).show().siblings().hide();
		}
	},2000)       // 每三秒执行一次

	/*最新咨询切换*/
	$('.new-advisory .next').click(function(){
      var margin = $('.new-advisory ul li').width()+30
		$('.new-advisory ul').animate({'marginLeft':-margin},function(){
			$(this).css('marginLeft',0).find('li:first').appendTo($(this));
		});
	});

	$('.new-advisory .prev').click(function(){
      var margin = $('.new-advisory ul li').width()+30
		$('.new-advisory ul').css('marginLeft',-margin).find('li:last').prependTo($('.new-advisory ul'));
		$('.new-advisory ul').animate({'marginLeft':0});
	});


	//最新资询
  get_question();
  function get_question(){
    var question_url = apitest+"/question?type=question&size=9";
    $.get(question_url, function(data){
    }, "json").error(function(data) {
        var status_code = $.parseJSON(data.responseText);
        layer.msg("")
    }).success(function(data) {
      	var obj = data.data;
        console.info(obj);
      	var str = '';
      		for(var i=0; i<obj.length;i++){
      			if (obj[i].creator_realname == null) {obj[i].creator_realname = '匿名用户'};
              str+='<li>'
                    +'<span class="color-bar"></span>'
                    +'<div class="text">'
                        +'<p class="title">'+obj[i].title+'</p>'
                        +'<p class="line-height name">'+obj[i].post_category_name+'</p>'
                        +'<p class="line-height">'
                            +'<a href="javascript:void(0)" data-href="'+obj[i].id+'" class="currentA See-details">查看详情&gt;&gt;</a>'
                        +'</p>'
                    +'</div>'
                +'</li>';
      			}
      		$("#listQuestions ul").append(str);


          $(".See-details").click( function (e) {
            var $this=$(this);
            var question_id = $this.attr("data-href");
            get_question_viwe(question_id);
          });
  	});
  }

  function get_question_viwe(question_id){
    var question_url_viwe = apitest+"/question/"+question_id;
    $.get(question_url_viwe, function(data){
    }, "json").error(function(data) {
        var status_code = $.parseJSON(data.responseText);
        layer.msg("")
    }).success(function(data) {
        var obj = data.data;
        console.info(obj);
        if (obj.contact_name==null) {obj.contact_name="匿名用户"}
        $(".name1").text(obj.contact_name);
        $(".title1").text(obj.title);
        $(".content1").text(obj.content);
        $(".time1").text(obj.created_at);
        $(".userImg01 img").attr("src",obj.creator_avatar);
        $(".Release-response").attr("data-id",obj.id);
    });

    var Popup01 = $(".Popup01");
    layer.open({
          type: 1, //基本层类型  可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）。 若你采用layer.open({type: 1})方式调用，则type为必填项（信息框除外）
          title: false, //标题
          closeBtn: true,  //关闭按钮
          shade:0.3,  //遮罩默认是0.3透明度
      area: ['840px', '560px'],
          skin: 'yourclass', //样式类名
          content: Popup01 //内容
    }); 
    layerCss();
  };

  // 发布回答输入款边款变色
  $(".textarea").focus(function(){  // 获得焦点
    $(".textarea").css("border","1px solid #48b4ff")
    $(".iconleft1").addClass("iconleft2").removeClass("iconleft1");
  }).blur(function(){     // 失去焦点
    $(".textarea").css("border","1px solid #e8e8e8");
    $(".iconleft2").addClass("iconleft1").removeClass("iconleft2");
  });

  // 发布回答
  $(".Release-response").click(function(){
      var comment = $(".textarea").val();
      if (comment<1) {
        layer.msg("回答内容不能为空");
        return false;
      }else if (comment>1000) {
        layer.msg("回答内容不能超过1000个字");
        return false;
      }else{
        var loading = layer.msg('操作进行中...', {icon: 16});
        var response_id = $(".Release-response").attr("data-id")
        var response_url = apitest+"/question/"+response_id+"/comment";
        $.post(response_url,{"comment":comment,"creator_id":user_id},function(data){
          },"json").error(function(data){
            var error =  $.parseJSON(data.responseText);
            layer.msg("出问题了");
          }).success(function(data){
            var obj = data.data;
            layer.msg("回答成功");
            location.href=location.href;
        });
      }

  });
  



});
